package com.jingyanzi.resallocation.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.jingyanzi.resallocation.dao.ResSceneOptionsMapper;
import com.jingyanzi.resallocation.domain.SceneOptionsDO;
import com.jingyanzi.resallocation.entity.ResSceneOptions;
import com.jingyanzi.resallocation.entity.ResSceneOptionsExample;
import com.jingyanzi.resallocation.entity.ResSceneOptionsExample.Criteria;
import com.jingyanzi.resallocation.query.SecneOptionsQueryDO;
import com.jingyanzi.resallocation.result.ResultDO;
import com.jingyanzi.resallocation.result.ResultSupport;
import com.jingyanzi.resallocation.service.ISceneOptionsService;
import com.jingyanzi.resallocation.util.BeanUtilsExtends;

public class SceneOptionsServiceImpl implements ISceneOptionsService {
	
	@Autowired
	private ResSceneOptionsMapper sceneOptionsMapper;

	@Override
	public ResultDO add(SceneOptionsDO domain) {
		
		ResultDO result = new ResultSupport();
		ResSceneOptions entity = new ResSceneOptions();
		
		if(!BeanUtilsExtends.copyProperties(entity, domain)) {
			result.setSuccess(false);
			return result;
		}
		
		int r = -1;
		
		entity.setCreateTime(Calendar.getInstance().getTime());
		entity.setIsDelete(1);
		
		try {
			r = sceneOptionsMapper.insertSelective(entity);
		} catch(Exception e) {
			e.printStackTrace();
			r = -1;
		}
		
		if(r < 0) {
			result.setSuccess(false);
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, entity.getId());
		return result;
	}

	@Override
	public ResultDO delete(Integer id) {

		ResultDO result = new ResultSupport();
		ResSceneOptions entity = new ResSceneOptions();
		
		entity.setId(id);
		entity.setIsDelete(-1);
		
		int r = -1;
		
		try {
			r = sceneOptionsMapper.updateByPrimaryKeySelective(entity);
		} catch(Exception e) {
			e.printStackTrace();
			r = -1;
		}
		
		if(r < 0) {
			result.setSuccess(false);
		}
		return result;
	}

	@Override
	public ResultDO edit(SceneOptionsDO domain) {
		
		ResultDO result = new ResultSupport();
		ResSceneOptions entity = new ResSceneOptions();
		
		if(!BeanUtilsExtends.copyProperties(entity, domain)) {
			result.setSuccess(false);
			return result;
		}
		
		int r = -1;
		
		try {
			r = sceneOptionsMapper.updateByPrimaryKeySelective(entity);
		} catch(Exception e) {
			e.printStackTrace();
			r = -1;
		}
		
		if(r < 0) {
			result.setSuccess(false);
		}
		
		return result;
	}

	@Override
	public ResultDO query(SecneOptionsQueryDO query) {

		ResultDO result = new ResultSupport();
		List<ResSceneOptions> list = null;
		
		ResSceneOptionsExample example = new ResSceneOptionsExample();
		Criteria criteria = example.createCriteria();
		if(query.getSceneId() != null && query.getSceneId() != 0) {
			criteria.andSceneIdEqualTo(query.getSceneId());
		}
		if(query.getSiteId() != null && query.getSiteId() != 0) {
			criteria.andSiteIdEqualTo(query.getSiteId());
		}
		if(StringUtils.isNotEmpty(query.getSiteName())) {
			criteria.andSiteNameLike("%" + query.getSiteName() + "%");
		}
		
		long count = 0;
		if(query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			count = sceneOptionsMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}
		
		try {
			list = sceneOptionsMapper.selectByExample(example);
		} catch(Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			return result;
		}
		return null;
	}

	@Override
	public ResultDO get(Integer id) {
		
		ResultDO result = new ResultSupport();
		ResSceneOptions entity = null;
	
		int r = -1;
		
		try {
			entity = sceneOptionsMapper.selectByPrimaryKey(id);
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		if(entity == null) {
			result.setSuccess(false);
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, getSceneOptionsDO(entity));
		return result;
	}
	
	private SceneOptionsDO getSceneOptionsDO(ResSceneOptions entity) {
		if (entity == null)
			return null;
		SceneOptionsDO dst = new SceneOptionsDO();
		dst = BeanUtilsExtends.copyProperties(dst, entity) ? dst : null;
		return dst;
	}

}
